﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BtoBSWT315.User
{
    public partial class SJManage : System.Web.UI.Page
    {
        public string sql1;
        int StartIndex = 0;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (Session["user"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            if (!IsPostBack)
            {
                DataGridDataBind();
            }
        }

        private void DataGridDataBind()
        {
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();

            //string sql = "select * from info  inner join qyml on info.gsid=qyml.id where qyml.user= '" + Session["user"] + "'" + " order by info.qorder asc, info.info_id desc";
            string sql = string.Empty;
            //TODO 二期工作，当搜索出结果只有一条的时候，而StartIndex=2，或者3的时候会出现CRASH
            sql = "select * from info  inner join qyml on info.gsid=qyml.id where qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_id desc";

            //switch (DropDownList1.SelectedValue.ToString())
            //{
            //    case "0":
            //        sql = "select * from info  inner join qyml on info.gsid=qyml.id where qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_id desc";
            //        break;
            //    case "采购":
            //        sql = "select * from info inner join qyml on info.gsid=qyml.id  where type='采购' and showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_ID desc";
            //        break;
            //    case "供应":
            //        sql = "select * from info inner join qyml on info.gsid=qyml.id  where type='供应' and showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_ID desc";
            //        break;
            //    case "合作":
            //        sql = "select * from info inner join qyml on info.gsid=qyml.id  where type='合作' and showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_ID desc";
            //        break;
            //    case "代理":
            //        sql = "select * from info inner join qyml on info.gsid=qyml.id  where type='代理' and showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_ID desc";
            //        break;
            //}

            //创建数据集
            DataSet ds = new DataSet();

            //从指定的索引开始取PageSize条记录
            classdb.Getda(sql).Fill(ds, StartIndex, DataGrid1.PageSize, "CurDataTable");
            //填充所有数集
            classdb.Getda(sql).Fill(ds, "AllDataTable");
            //设置DataGrid控件实际显示的项数
            DataGrid1.VirtualItemCount = ds.Tables["AllDataTable"].Rows.Count;

            //绑定数据源
            DataGrid1.DataSource = ds.Tables["CurDataTable"];
            DataGrid1.DataBind();
        }

        private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            //设置DataGrid当前页的索引值为用户选择的页的索引
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            //取得当前页为止总共有多少条记录，以便在下一页就从该记录开始读取
            StartIndex = DataGrid1.PageSize * DataGrid1.CurrentPageIndex;
            //重新绑定数据
            DataGridDataBind();
        }


        private void changeRowColor(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            //如果是数据项并且是交替项
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //添加自定义属性，当鼠标移过来时设置该行的背景色为"E8F4FF",并保存原背景色
                e.Item.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='E8F4FF'");
                //添加自定义属性，当鼠标移走时还原该行的背景色
                e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");

            }

        }



        private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            //判断如果是DataGrid控件中的项、交替项以及编辑项
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
            {
                TableCell myTableCell;
                //定义对“删除”单元格的引用
                myTableCell = e.Item.Cells[6];
                //定义对“删除”单元格中控件的引用
                LinkButton btnDel = (LinkButton)myTableCell.Controls[0];
                //添加当单击时弹出确认对话框的自定义属性
                btnDel.Attributes.Add("onclick", "return confirm('您真的要删除此行吗？');");
                btnDel.Text = "<img src=images/del.gif border=0>";
            }
        }



        private void DataGrid1_DeleteCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if (Session["user"] != null)
            {
                int empID = (int)DataGrid1.DataKeys[e.Item.ItemIndex];
                //定义所需SQL语句
                string sql = "delete DISTINCTROW info.* from info inner JOIN  qyml on info.gsid=qyml.id where info.info_id=" + empID + "";
                //string sql = "delete from info where info_ID=" + empID + "";
                //引入数据库操作类
                Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
                classdb.GetNonResult(sql);

                //重新绑定
                DataGridDataBind();
            }
            else
            {
                Response.Redirect("../Default.aspx");
            }
        }

        private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (Session["user"] != null)
            {
                switch (DropDownList1.SelectedValue.ToString())
                {

                    case "0":
                        //sql1 = "select * from info where flag=1 order by qorder asc,info_id desc";
                        sql1 = "select * from info  inner join qyml on info.gsid=qyml.id where qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_id desc";
                        break;
                    case "采购":
                        //sql1 = "select * from info where showname like '%" + TextBox1.Text + "%' and flag=1 and type='采购' order by qorder asc,info_id desc";
                        sql1 = "select * from info inner join qyml on info.gsid=qyml.id  where type='采购' and showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_ID desc";
                        break;
                    case "供应":
                        //sql1 = "select * from info where showname like '%" + TextBox1.Text + "%' and flag=1 and type='供应'order by qorder asc,info_id desc";
                        sql1 = "select * from info inner join qyml on info.gsid=qyml.id  where type='供应' and showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_ID desc";
                        break;
                    case "合作":
                        //sql1 = "select * from info where showname like '%" + TextBox1.Text + "%' and flag=1 and type='合作'order by qorder asc,info_id desc";
                        sql1 = "select * from info inner join qyml on info.gsid=qyml.id  where type='合作' and showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_ID desc";
                        break;
                    case "代理":
                        //sql1 = "select * from info where showname like '%" + TextBox1.Text + "%' and flag=1 and type='代理'order by qorder asc,info_id desc";
                        sql1 = "select * from info inner join qyml on info.gsid=qyml.id  where type='代理' and showname like '%" + TextBox1.Text + "%' and qyml.user='" + Session["user"] + "'" + " order by info.qorder asc,info.info_ID desc";
                        break;

                }
                //引入数据库操作类
                Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
                //绑定数据源
                DataGrid1.DataSource = classdb.GetTables(sql1).DefaultView;
                DataGrid1.DataBind();
            }
            else
            {
                Response.Redirect("../Default.aspx");
            }
       
        }
    

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
            this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
            this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
            this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
            this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.changeRowColor);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}